package com.shenzhenai.composesideeffects.samples

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Warning
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.shenzhenai.composesideeffects.repository.Image
import com.shenzhenai.composesideeffects.repository.ImageRepository
import com.shenzhenai.composesideeffects.repository.Result
import com.shenzhenai.composesideeffects.ui.theme.DN_Compose_LearnTheme

/**
 * @author huangzehui
 * @date 20/9/2025 下午5:39 周六
 * @description
 *
 * 84-produceState
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=84
 *
 */

@SuppressLint("ProduceStateDoesNotAssignValue")
@Composable
fun loadNetworkImage(url: String, imageRepository: ImageRepository): State<Result<Image>> {
    return produceState(
        initialValue = Result.Loading as Result<Image>,
        key1 = url,
        key2 = imageRepository
    ) {
        Log.e("young_tesjkjdkf", "thread:${Thread.currentThread().name}")
        val image = imageRepository.load(url)
        // 这里将值复制给 produceState 的 value，就是一个状态了，
        // 因为 produceState 这个本身就是状态了
        value = if (image == null) Result.Error else Result.Success(image)
    }
}

@Composable
fun ProduceStateSample() {

    val imageList = listOf(
        "https://tse1.mm.bing.net/th/id/OIP.rAzIUHScvAJZYEnsOYB3oAHaNK?rs=1&pid=ImgDetMain&o=7&rm=3",
        "https://c-ssl.dtstatic.com/uploads/blog/202405/03/73SmDPGxIeB0Gel.thumb.1000_0.jpg",
        "https://故意img0.baidu.com/it/u=715533040,1196819708&fm=253&app=138&f=JPEG?w=800&h=1449",
    )

    var index by remember {
        mutableStateOf(0)
    }


    val imageRepository = ImageRepository(LocalContext.current)
    val result = loadNetworkImage(url = imageList[index], imageRepository)

    Column {
        Button(onClick = {
            index %= imageList.size
            if (++index == imageList.size) index = 0
        })
        {
            Text(text = "选择第${index}张图片")
        }

        // Image
        when (result.value) {
            is Result.Success -> {
                androidx.compose.foundation.Image(
                    bitmap = (result.value as Result.Success).image.imageBitmap,
                    contentDescription = "image load success"
                )

            }

            is Result.Error -> {
                androidx.compose.foundation.Image(
                    imageVector = Icons.Rounded.Warning,
                    contentDescription = "image load error",
                    modifier = Modifier.size(200.dp, 200.dp)
                )
            }

            else -> {
                // 进度条
                CircularProgressIndicator()
            }

        }
    }
}


@Preview
@Composable
fun ProduceStateSamplePreview() {
    DN_Compose_LearnTheme {
        ProduceStateSample()
    }
}